package com.demo.springmvc.converter;

import com.demo.springmvc.pojo.Address;
import com.demo.springmvc.pojo.User;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class UserConverter implements Converter<String, User> {

    @Override
    public User convert(String source) {
        if (source != null) {
            String[] prop = source.split("-");
            if (prop.length == 3) {
                User user = new User();
                user.setUsername(prop[0]);
                user.setAge(Integer.parseInt(prop[1]));
                user.setAddress(new Address().setCity(prop[2]));
                return user;
            }
        }
        return null;
    }
}
